/*=============================================================================

  DEVICE STATE MODULE

  FILE: SYSmgr_S_DEV.cpp
  
  DESCRIPTION

    Device State Implementation

  EXTERNALIZED FUNCTIONS

    
  Copyright (c) DaeSung Engineering Inc. All rights reserved.

=============================================================================*/
/*=============================================================================

                             EDIT HISTORY

  ---------  -------  -------------------------------------------------------
    when       who     what, where, why
  ---------  -------  -------------------------------------------------------
   06/28/03   nso4th   Version 0.1

=============================================================================*/

/*=============================================================================
  HEADER INCLUSION
=============================================================================*/
#include <SYSmgr.h>

/*=============================================================================
  CONSTANT DEFINITION
=============================================================================*/

/*=============================================================================
  LOCALIZED VARIABLE DEFINITION
=============================================================================*/
static dword	gStep = STATE_INIT_STEP;
#define SETSTEP(s)		((gStep) = (s))
#define GETSTEP()		(gStep)

/*=============================================================================
  EVENT PROCESSING
=============================================================================*/
dword SYSmgr::DEV_STATE(dword dwType, dword dwEvent, dword wParam, dword lParam)
{
	dword		st = m_mnState[DEV_S];
	
	switch(st)
	{
	case DEV_IDL_S:
		return	DEV_ProcIDL(dwType, dwEvent, wParam, lParam);
	
	case DEV_INIT_S:
		return	DEV_ProcINIT(dwType, dwEvent, wParam, lParam);

	case DEV_CLOSE_S:
		return	DEV_ProcCLOSE(dwType, dwEvent, wParam, lParam);
	}

	return	FALSE;
}

dword SYSmgr::DEV_ProcIDL(dword dwType, dword dwEvent, dword wParam, dword lParam)
{
	if(IS_CMD_EVENT(dwType) == FALSE)
		return	FALSE;
	
	switch(dwEvent)
	{
	case DEVINIT_C:
	{
		SETSTEP(STATE_INIT_STEP);
		State_Minor(DEV_INIT_S);
		break;
	}
	case DEVCLOSE_C:
	{
		SETSTEP(STATE_INIT_STEP);
		State_Minor(DEV_CLOSE_S);
		break;
	}
	case DEVRESET_C:
	{
		m_devReset = TRUE;
		if(m_devInit)
		{
			SETSTEP(STATE_INIT_STEP);
			State_Minor(DEV_CLOSE_S);
		}
		else
		{
			SETSTEP(STATE_INIT_STEP);
			State_Minor(DEV_INIT_S);
		}

		break;
	}
	}

	return	TRUE;
}

dword SYSmgr::DEV_ProcINIT(dword dwType, dword dwEvent, dword wParam, dword lParam)
{
	boolean			bDone = FALSE;
	static boolean		bDevInit = TRUE;
	dword			event, evtID;

	event = HDEV_GETEVENT(dwEvent);
	evtID = HDEV_GETDEV_ID(dwEvent);

	if(IS_STATE_EVENT(dwType))
	{
		// Next
	}
	else
	{
		return	FALSE;
	}

	switch(GETSTEP())
	{
	case DEVINIT_MOD:
	{
		Event_Handler(SYS_MGR, DEVINIT_START_E, 0, TRUE);
		MGR_Lock(TRUE);
		MON_LOCK(TRUE);
		
		if(!MOD_Init())
		{
			bDevInit = FALSE;
		}
		break;
	}
	case DEVINIT_HDIO:
	{
		if(!HIO_Init())
		{
			bDevInit = FALSE;
		}
		break;
	}
	case DEVINIT_HAIO:
	{
		if(!HAIO_Init())
		{
			bDevInit = FALSE;
		}
		break;
	}
	case DEVINIT_HMOT:
	{
		if(!HMot_Init())
		{
			bDevInit = FALSE;
		}
		break;
	}
	case DEVINIT_HFG:
	{
		if(!HFg_Init())
		{
			bDevInit = FALSE;
		}
		break;
	}
	case DEVINIT_HCAM:
	{
		if(!HCam_Init())
		{
			bDevInit = FALSE;
		}
		bDone = TRUE;
		break;
	}
	}
	
	if(bDone)
	{
		MGR_Lock(FALSE);
		MON_LOCK(FALSE);

#ifdef	TG_SYSMGR_SIMUL
		SYS_DELAY(1000);
#endif

		if(bDevInit)
		{
			m_devInit = TRUE;
			Event_Handler(SYS_MGR, DEVINIT_DONE_E, 0, 0);
			m_devReset = FALSE;

			SETSTEP(DEVINIT_BASE);
			State_Change(IDL_S, IDL_IDL_S);
			//State_Change(RESET_S, RESET_RESET_S);
		}
		else
		{
			m_devInit = FALSE;
			bDevInit = TRUE;
			Event_Handler(SYS_MGR, ERR_DEVINIT_E, 0, 0);
			m_devReset = FALSE;
			SETSTEP(DEVINIT_BASE);
			State_Change(IDL_S, IDL_ERR_S);
		}
	}
	else if(IS_STATE_EVENT(dwType))
	{
		gStep ++;
	}

	return	FALSE;
}

dword SYSmgr::DEV_ProcCLOSE(dword dwType, dword dwEvent, dword wParam, dword lParam)
{
	boolean			bDone = FALSE;
	static boolean		bDevClose = TRUE;

	if(!IS_STATE_EVENT(dwType))
	{
		return	FALSE;
	}

	switch(GETSTEP())
	{
	case DEVCLOSE_HCAM:
	{
		Event_Handler(SYS_MGR, DEVCLOSE_START_E, 0, TRUE);
		MON_LOCK(TRUE);
		MGR_Lock(TRUE);

		if(!HCam_Close())
		{
			bDevClose = FALSE;
		}
		break;
	}
	case DEVCLOSE_HFG:
	{
		if(!HFg_Close())
		{
			bDevClose = FALSE;
		}
		break;
	}
	case DEVCLOSE_HMOT:
	{
		if(!HMot_Close())
		{
			bDevClose = FALSE;
		}
		break;
	}
	case DEVCLOSE_HAIO:
	{
		if(!HAIO_Close())
		{
			bDevClose = FALSE;
		}
		break;
	}
	case DEVCLOSE_HDIO:
	{
		if(!HIO_Close())
		{
			bDevClose = FALSE;
		}
		break;
	}
	case DEVCLOSE_MOD:
	{
		if(!MOD_Close())
		{
			bDevClose = FALSE;
		}
		bDone = TRUE;
		break;
	}
	}

	if(bDone)
	{
		SETSTEP(DEVCLOSE_BASE);
		m_devInit = FALSE;
		if(bDevClose)
		{
			Event_Handler(SYS_MGR, DEVCLOSE_DONE_E, 0, 0);
		}
		else
		{
			bDevClose = TRUE;
			Event_Handler(SYS_MGR, ERR_DEVCLOSE_E, 0, 0);
		}
		if(m_devReset)
		{
			SYS_DELAY(200);
			State_Change(DEV_S, DEV_INIT_S);
		}
		else
		{
			State_Change(IDL_S, IDL_IDL_S);
		}
	}
	else if(IS_STATE_EVENT(dwType))
	{
		gStep ++;
	}

	return	FALSE;
}

/*--- END OF SYSmgr_S_DEV.CPP ---*/
